package com.is.add.event;

import com.is.add.service.IApiLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author: zhou
 * @Description:
 * @Date:Create in 2019/07/01
 * @Modified By:
 */
@Component
public class ApiLogListener {

    private static final Logger log = LoggerFactory.getLogger(ApiLogListener.class);

    @Autowired
    private IApiLogService apiLogService;

    //TODO 还需要测试异步
    @Async
    @Order
    @EventListener({ApiLogEvent.class})
    public void saveApiLog(ApiLogEvent event) {
        try{
            Map<String, Object> source = (Map)event.getSource();
            LogApi logApi = (LogApi)source.get("log");
            log.warn("增加操作日志toDB");
            this.apiLogService.saveApiLog(logApi);
        }catch (Exception e){
            log.error("插入操作日志异常",e);
        }
    }
}
